<?php
namespace Home\Controller;
use Think\Controller;
class PersonController extends CommonController {
	//个人账户
    public function index(){
    	$this -> assign('titile','个人账户');
    	$userMod = M('users');

    	$userdata = $userMod -> where('');
        $this->display('');
    }
   
    public function code(){
			$Verify = new \Think\Verify();
			$Verify->fontSize =  20;    // 大小  
			$Verify->length   =    4;    // 个数   
			$Verify->useNoise =   false; // 有无点啥的	
			$Verify->codeSet = '0123456789'; //设置为纯数字	
			$Verify->entry();
	}
	
	//处理用户密码	
    public function dologin(){

    	//获取验证码
    	$Verify = new \Think\Verify();
		$res = $Verify->check(I('post.code'));		
		if($res){
			
				if(empty(session('users.ec_salt'))){
						$password = md5(I('post.password'));
						if(session('users.password')==$password){
							echo '1';
							
						}else{
							echo '22';
						}

					}else{
						$password = md5(md5(I('post.password')).session('users.ec_salt'));
						if(session('users.password')==$password){
							echo '1';
							
						}else{
							echo '22';
						}	

					}
			
		}else{
			echo '2';
		}
	}
	public function detail(){

		//查询会员等级
		$type = session('users.user_type');
		$mod = M('user_type');
		$vip = $mod -> where('id='.$type)->find()['name'];
		//查询推荐人信息
		$userMod  = M('users');
		$inviterName = $userMod -> where('user_id='.session('users.inviter_id'))->find()['user_name'];
		$treeName = $userMod -> where('user_id='.session('users.tree_id'))->find()['user_name'];
		//查询接点人信息
		$this -> assign('vip',$vip);
		$this -> assign('inviterName',$inviterName);
		$this -> assign('treeName',$treeName);
		$this -> display('');
	}

	public function dopersonAjax(){
		if(md5(I('post.repassword'))==session('users.repassword')){
				echo '1';
		}else{
				echo '2';
		}
	}

	//处理修改信息
	public function doperson(){
		
		$_SESSION['users']['mainbank'] = I('post.mainbank');
		$_SESSION['users']['bank_code'] = I('post.bank_code');
		$_SESSION['users']['bank_name'] = I('post.bank_name');
		$_SESSION['users']['zhifubao'] =I('post.zhifubao');
		$_SESSION['users']['weixin'] = I('post.weixin');
		$user = $_SESSION['users'];
		//实例化数据库
		$mod = M('users');

		$res = $mod -> where('user_id='.$user['user_id'])->save($user);
		if($res>0){
			$this -> success('修改成功',U('home/person/detail'));
		}else{
			$this -> error('修改失败',U('home/person/detail'));
		}
	}




/*
	安全中心

*/

	//安全中心
	public function phone(){
		$this -> display('');
	}

	//获得验证码
	public function phoneAjax(){
		if(I('post.code')==1){			
				$_SESSION['mobile']['code'] = rand('111111','999999');
				$_SESSION['mobile']['mobile'] =  $_SESSION['users']['mobile_phone'];				
				$_SESSION['mobile']['content']=",您的短信验证码为".$_SESSION['mobile']['code']."，仅用于修改密码、卖出现金积分操作，短信验证码的有效时间为5分钟，使用后即作废。";
			
			$this -> send_msg();
			
		}else{
			echo  '2';
		}

	}
	//修改安全密码
	public function editRepassword(){
		$mod = M('users');
		if(I('post.mobile_code')==$_SESSION['mobile']['code']){
			$data['user_id'] = $_SESSION['users']['user_id'];
			$_SESSION['users']['repassword'] = md5(I('post.repassword1'));
			$res = $mod -> where($data)->save($_SESSION['users']);
			if($res > 0){
				session('mobile',null);				
				echo '1';
			}else{
				echo '2';
			}
		}else{
			echo '22';
		}
	}

	public function dopassword(){
		$repassword = I('post.repassword');		
		$newpassword = I('post.newpassword');
		$newpassword2 = I('post.newpassword2');
		$user = $_SESSION['users'];
	//判断加密字段是否存在
		if(empty($user['ec_salt'])){
			$password = md5(I('post.password'));
			if($user['password']==$password){
				if(md5($repassword)==$user['repassword']){
					if($newpassword==$newpassword2){
						$user['ec_salt'] = rand('1111','9999');
						$user['password'] = md5(md5(I('post.newpassword')).$user['ec_salt']);
						$mod = M('users');
						
							//修改数据
							$res = $mod -> where('user_id='.$user['user_id'])->save($user);

								if($res>0){									
									$this -> success('密码修改成功,下次登录生效');
								}else{
									$this -> error('修改失败');
								}
					}else{
						echo'222';
					}
				}else{
					echo '2';
				}

			}else{
				echo '22';
			}

		}else{
			$password = md5(md5(I('post.password')).$user['ec_salt']);
			if($user['password']==$password){
				if(md5($repassword)==$user['repassword']){
					if($newpassword==$newpassword2){
						$user['ec_salt'] = rand('1111','9999');
						$user['password'] = md5(md5(I('post.newpassword')).$user['ec_salt']);
						$mod = M('users');						
							//修改数据

							$res = $mod -> where('user_id='.$user['user_id'])->save($user);
								if($res>0){

								
									$this -> success('密码修改成功,下次登录生效');
								}else{
									$this -> error('修改失败');
								}
					}else{
						echo'222';
					}
				}else{
					echo '2';
				}

			}else{
				echo '22';
			}

		

		}

	}

	//封装好的发送短信接口
	public function send_msg(){
				    // //$msgusrl="http://222.73.117.156/msg/HttpBatchSendSM?account=vip_lxgj66&pswd=O3l7Nhs8k&mobile=".$msg_mobile."&msg=".$msg_content."&needstatus=true";
				    // $url ="http://api.sms.cn/msg";
				   
				    // $postFields=array (
								//           'un' => 'dong354698601',
								//           'pw' => 'dong6395175346',
								//           'msg' => $msg_content,
								//           'phone' => $msg_mobile,
								//           'rd' => 1
				    //                  );
				    // curlPost($url,$postFields);
		//			
					//接口账号
					$uid = 'dong354698601';

					//登录密码
					$pwd = 'dong6395175346';

					/**
					* 实例化接口
					* 
					* @param string $uid 接口账号
					* @param string $pwd 接口密码					*/
				
					$api = new \Org\Util\Smsapi($uid,$pwd);

					$mobile = $_SESSION['mobile']['mobile'];

					//短信内容参数
					$contentParam = array(
						'code'		=> $_SESSION['mobile']['code'],
						'username'	=> $_SESSION['mobile']['content']
						);

					//变量模板ID
					$template = '397816';

					//发送变量模板短信
					$result = $api->send($mobile,$contentParam,$template);

					if($result['stat']=='100')
					{
						echo '发送成功';
					}
					else
					{
						echo '发送失败:'.$result['stat'].'('.$result['message'].')';
					}

		}




}